const Base = require("./base.js");

module.exports = class extends Base {
  async usergetAction() {
    const coupon = await this.model("user_coupon").thenAdd(
      {
        user_id: this.getLoginUserId(),
        coupon_id: this.post("coupon_id"),
        coupon_number: 1
      },
      {
        user_id: this.getLoginUserId(),
        coupon_id: this.post("coupon_id")
        // order_id: 0
      }
    );
    return this.success(coupon);
  }
  async getmanyAction() {
    const coupon = await this.model("user_coupon").addMany(
      this.post("coupons")
    );
    return this.success(coupon);
  }
  async listAction() {
    const list = await this.model("coupon")
      .where({ is_used: 1,use_end_date:['>=',this.getTime()] })
      .select();
    return this.success(list);
  }
  async detailAction() {
    const couponId = this.post("couponid");
    const coupons = await this.model("coupon")
      .where({ id: ["IN", couponId.split(",")], is_used: 1,use_end_date:['>=',this.getTime()] })
      .select();

    return this.success(coupons);
  }
};
